package com.yzs.core.base

import android.view.LayoutInflater
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding
import com.yzs.base.base.BaseVBVMActivity
import com.yzs.core.bean.NetworkViewState
import com.yzs.core.bean.PageStatus
import kotlin.reflect.KClass

/**
 *    author : yzs
 *    date   : 2022/1/1316:04
 *    desc   : 业务层提供便捷使用 viewBinding、viewModel
 *    子类不重写 initViewModel(), 重写 observe() 在该方法中对 vm 进行监听和调用
 *    例如: class MainActivity : CoreBaseVBVMActivity<ActivityMainBinding, MainVM>(ActivityMainBinding::inflate, MainVM::class.java){}
 */
abstract class CoreBaseVBVMActivity<VB : ViewBinding, VM : ViewModel>(
    inflate: (LayoutInflater) -> VB,
    clazz: KClass<VM>
) : BaseVBVMActivity<VB, VM>(inflate, clazz) {

    /**
     * 在基类统一处理加载框
     * 如果不需要统一处理，可以重写该方法 不调用 super
     */
    open fun setPageStateLoadingUI(viewStates: LiveData<NetworkViewState>) {
        viewStates.observeKtNoNull {
            when (it.pageStatus) {
                is PageStatus.LOADING -> showLoadingDialog()
                is PageStatus.SUCCESS -> dismissLoadingDialog()
                is PageStatus.FAILURE -> dismissLoadingDialog()
            }
        }
    }
}